package com.cms.plugins.seo_url;


import com.cms.common.context.ApplicationContextProvider;
import com.cms.common.hook.config.Filed;
import com.cms.common.hook.target.BootstrapTarget;
import com.cms.common.hook.target.Hook;
import com.cms.common.hook.vo.HookParams;
import com.cms.common.hook.vo.HookResult;
import com.cms.common.model.entity.Plugin;
import com.cms.common.model.services.PluginBootstrap;
import com.cms.common.model.vo.Menu;
import com.cms.html.HtmlRenderService;
import com.gitee.starblues.bootstrap.PluginContextHolder;
import com.gitee.starblues.core.PluginExtensionInfo;
import com.gitee.starblues.core.descriptor.InsidePluginDescriptor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@BootstrapTarget
@Component
public class BootstrapPlugin implements PluginBootstrap, PluginExtensionInfo {


    @Hook(hook_key = "admin.login.test.response.html")
    public HookResult test(HookParams params) {

        HtmlRenderService htmlRenderService = ApplicationContextProvider.getBean(HtmlRenderService.class);
        System.out.println("plugin hook test");
        String html = (String) params.getRequestParams().get("html");
        html = htmlRenderService.insertBefore(html, "el-input[name=\"test\"]", "00000000000000000000000");
        html = htmlRenderService.insertAfter(html, "el-input[name=\"test\"]", "111111111111111111111111");
        params.getRequestParams().put("html", html);
        return new HookResult();
    }

    @Hook(hook_key = "admin.menus")
    public HookResult addMenu(HookParams params) {
        List<Menu> menus = (List<Menu>) params.getRequestParams().get("menuList");
        menus.add(Menu.createMenu("插件菜单和页面", "", "/plugins/plugins-feature-seo-url/admin/seo/url", null, ""));
        params.getRequestParams().put("menuList", menus);
        return new HookResult();
    }

    @Hook(hook_key = "admin.login.test1")
    public HookResult hookTest(HookParams params) {

        HtmlRenderService htmlRenderService = ApplicationContextProvider.getBean(HtmlRenderService.class);
        System.out.println("plugin hook test");
        String html = (String) params.getRequestParams().get("html") + params.getRequestParams().get("data-test");
        params.getRequestParams().put("html", html);
        return new HookResult();
    }

    @Hook(hook_key = "admin.login.test2")
    public HookResult hookTest2(HookParams params) {

        System.out.println("plugin hook test");
        String html = "" + params.getRequestParams().get("html");
        html = html + (String) params.getRequestParams().get("html") + params.getRequestParams().get("data-test");
        params.getRequestParams().put("html", html);
        return new HookResult();
    }


    public Plugin config() {
        Plugin plugins = new Plugin();
        InsidePluginDescriptor descriptor = PluginContextHolder.getPluginDescriptor();
        plugins.setPluginId(descriptor.getPluginId());
        plugins.setName("SEO Url");
        plugins.setLogo("");
        plugins.setDescription("增加seo数据");

        return plugins;
    }

    public Map<String, Object> extensionInfo() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Filed> fields = new ArrayList<Filed>();

        map.put("plugin2", "插件信息2");
        return map;
    }
}
